<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\Service\HandleCash;
use App\Admin\Forms\Service\HandleWorker;
use App\Admin\Repositories\CashOrder;
use App\Admin\Repositories\GoodOrder;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Contracts\Support\Renderable;

class CashOrderController extends AdminController
{

    /**
     * Make a grid builder.
     *
     */
    protected function grid()
    {
        $row = new Row;
        $row->column(12,Card::make($this->order()));
        return $row;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function order()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->orderByDesc("id");
            $grid->column("shop.name","门店名称");
//            $grid->wrap(function (Renderable $view) {
//                $tab = Tab::make();
////                $count = \App\Models\CashOrder::query()->where("type",0)->where("status",0)->count();
////                $tab->add("门店提现订单({$count})", $view, true);
////                $count1 = \App\Models\CashOrder::query()->where("type",1)->where("status",0)->count();
////                $tab->add("员工提现订单({$count1})", $this->worker());
//                $count2 = \App\Models\CashOrder::query()->where("type",2)->where("status",0)->count();
//                $tab->add("代理商订单({$count2})", $this->agent());
//                return $tab;
//            });
        });
    }


    protected function makeGrid(\Closure $callback, $type = 0): Grid
    {
        return Grid::make(new CashOrder(['shop','user','worker','agent']), function (Grid $grid) use ($callback, $type) {
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableRowSelector();
            $this->common($grid);
            $callback($grid);
            $options = ['待审核','审核通过','审核拒绝'];
            $labels = ['default','success','danger'];
            $grid->column('status')->if(function ($column) {
                return $column->getValue() == 0;
            })->using($options)->label($labels)->modal("审核",function (){
                return HandleCash::make()->payload(['oid'=>$this->id]);
            })->else()->using($options)->label($labels);
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) use ($type) {
                $filter->expand(false);
                $filter->equal('id');
                $filter->like('out_trade_no','提现单号');
                $filter->equal('user_id','用户ID');
                $filter->like('user.nickname','用户昵称');
                if ($type==0){
                    $filter->like('shop.name','门店名称');
                }elseif ($type==1){
                    $filter->like('worker.name','员工名称');
                }else{
                    $filter->like('worker.name','代理商名称');
                }
                $filter->like('banks->bank_name','开户行名称');
                $filter->like('banks->bank_num','银行卡号');
                $filter->like('banks->name','开户姓名');
            });
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('status', '审核状态', ['待审核','审核通过','审核拒绝']);
            });
        });
    }

    protected function common(Grid $grid){
        $grid->column('out_trade_no');
        $grid->column('user_id');
        $grid->column('user.nickname','用户昵称');
        $grid->column('amount');
        $grid->column('banks.bank_name','开户行名称');
        $grid->column('banks.bank_num','银行卡号');
        $grid->column('banks.name','开户姓名');
    }

    /**
     * 员工订单
     *
     * @return Grid
     */
    protected function worker(): Grid
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("type",1);
            $grid->column("worker.name","员工名称");
        },1);
    }

    /**
     * 员工订单
     *
     * @return Grid
     */
    protected function agent(): Grid
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("type",2);
            $grid->column("agent.name","代理商名称");
        },2);
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CashOrder('user'), function (Form $form) {
            $form->display('id');
            $form->display('out_trade_no');
            $form->display('user_id');
            $form->display('user.nickname');
            $form->display('amount');
            $status = $form->model()->status;
            if (!$status){
                $form->radio("status","审核状态")->options([0=>'待审核',1=>"审核通过",2=>'审核拒绝']);
            }else{
                $form->display('status',"审核状态")->with(function ($value){
                    return $value==1?"审核通过":"审核拒绝";
                });
            }
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
